<?php

namespace App\Jobs;

use App\Models\Active;
use App\Models\Image as ImageModel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ImageUpdatedJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $img;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->img=ImageModel::pluck("url");
        $url=Cache::get("url");
        if ($url){
            Cache::forget("url");
            $this->url($url);
        }

        $qedit=Cache::get("qedit");
        $oqedit=Cache::get("oqedit");
        if ($qedit&&$oqedit){
            Cache::forget("qedit");
            Cache::forget("oqedit");
            $this->check_qedit($oqedit,$oqedit);
        }

    }

    private function check_qedit($edit,$nedit)
    {
        $model=new ImageModel();
        foreach ($this->img as $key=>$value){
            if(strpos($edit,$value)&&!strpos($nedit,$value)){
               $id=$model->where("url",$value)->value("id");
               $model->find($id)->delete();
            }
        }
    }


    private function url($url)
    {
        $model=new ImageModel();
        $id=$model->where("url",$url)->value("id");
        if ($id){
            $model->find($id)->delete();
        }
    }



}
